package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.qo.ProductQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import java.util.Optional;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ServletContext ctx;
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") ProductQueryObject qo, Model model){
        PageResult result = productService.query(qo);
        model.addAttribute("brands",brandService.list());
        model.addAttribute("result",result);
        return "product/list";
    }
    @RequestMapping("input")
    public String input(Long id,Model model){
        model.addAttribute("brands",brandService.list());
        if (Optional.ofNullable(id).isPresent()) {
            model.addAttribute("entity",productService.get(id));
        }
        return "product/input";
    }

    @RequiredPermission("删除商品")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        //删除图片前先将图片地址查出来存好
        String imagePath = productService.get(id).getImagePath();
        if (Optional.ofNullable(id).isPresent()) {
            productService.delete(id);
        }
        //删除商品后将服务器上的图片也删掉
        if (StringUtil.hasLength(imagePath)) {
            UploadUtil.deleteFile(ctx,imagePath);
        }
        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Product entity, MultipartFile pic) {
        if (pic != null && pic.getContentType().startsWith("image") && pic.getSize() > 0) {
            //判断是否需要删除旧的图片
            if (StringUtil.hasLength(entity.getImagePath())) {
                UploadUtil.deleteFile(ctx,entity.getImagePath());
            }
            //符合上传图片的要求,就上传
            String path = UploadUtil.upload(pic,ctx.getRealPath("/upload"));
            entity.setImagePath(path);
        }
        productService.saveOrUpdate(entity);
        return new JSONResult();
    }

    @RequestMapping("listView")
    public String listView(@ModelAttribute("qo") ProductQueryObject qo, Model model){
        //qo.setPageSize(20);
        model.addAttribute("brands",brandService.list());
        model.addAttribute("result",productService.query(qo));
        return "product/listView";
    }
}
